package com.wsx.sky.sentinel.third;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;

import java.util.concurrent.TimeUnit;

/**
 * @author wangshuaixin
 * @Title: com.wsx.sky.sentinel.third.UserService
 * @Description: TODO
 * @date 2019/03/06
 */
public class UserService {

    public static final String USER_RS = "userDegrade";


    public com.wsx.sky.sentinel.fisrt.UserService.User getUser(long uid) {
        Entry entry = null;

        try {
            TimeUnit.MILLISECONDS.sleep(5);
            entry = SphU.entry(USER_RS, EntryType.IN);

            TimeUnit.MILLISECONDS.sleep(100);
            return new com.wsx.sky.sentinel.fisrt.UserService.User("user-" + uid, uid);

        } catch (Throwable t) {
            long time = System.currentTimeMillis();
            //熔断降级的跟踪统计服务
            if (!BlockException.isBlockException(t)) {
                Tracer.trace(t);
                System.out.println("被熔断了1:" + time);
            } else {
                if (DegradeException.isBlockException(t)) {
                    System.out.println("被熔断了2:" + time);
                } else {
                    System.out.println("其他异常" + time);
                }

            }



        } finally {
            if (null != entry) {
                entry.exit();
            }
        }
        return null;
    }
}
